package com.zhijiang.hospital.service.impl;

import com.zhijiang.common.util.QRCode.QRCodeUtil;
import com.zhijiang.hospital.entrty.ComEmployee;
import com.zhijiang.hospital.entrty.dto.MeetingEmployeeDto;
import com.zhijiang.hospital.mapper.EmpolyeeMapper;
import com.zhijiang.hospital.mapper.MeetingEmployeeMapper;
import com.zhijiang.hospital.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DeptServiceImpl implements DeptService {

    @Autowired
    MeetingEmployeeMapper meetingEmployeeMapper;
    @Autowired
    EmpolyeeMapper empolyeeMapper;
    @Override
    public List<MeetingEmployeeDto> queryAll() {


        List<MeetingEmployeeDto> emplyeeDept = meetingEmployeeMapper.getAll();



//        emplyeeDept.stream().forEach(s->{
//            List<ComEmployee> comEmployee = empolyeeMapper.getAll(s.getDeptCode());
//            if(comEmployee.size()>0){
//                s.setComEmployees(comEmployee);
//            }
//        });

        for (MeetingEmployeeDto dto : emplyeeDept) {
            List<ComEmployee> comEmployee = empolyeeMapper.getAll(dto.getDeptCode());

            if(comEmployee.size()>0){
                dto.setComEmployees(comEmployee);
                dto.setId(dto.getDeptCode());
                dto.setLabel(dto.getDeptName());

                for (ComEmployee employee : comEmployee) {

                    employee.setLabel(employee.getEmplName());
                    employee.setId(employee.getEmplCode());
                }
            }
        }
        MeetingEmployeeDto meetingEmployeeDto = new MeetingEmployeeDto();
        meetingEmployeeDto.setLabel("中层干部");
        meetingEmployeeDto.setId("055");
        List<ComEmployee> comEmployees = empolyeeMapper.getCadre();
        for (ComEmployee comEmployee : comEmployees) {
            comEmployee.setLabel(comEmployee.getEmplName());
            comEmployee.setId(comEmployee.getEmplCode());
        }
        meetingEmployeeDto.setComEmployees(comEmployees);
        emplyeeDept.add(meetingEmployeeDto);


        List<MeetingEmployeeDto> collect = emplyeeDept.stream().filter(s -> s.getComEmployees() != null).collect(Collectors.toList());
        log.info("部门："+collect.size());
        return collect;
    }


    @Override
    public ComEmployee get(String mOperCode) {
        ComEmployee c =  meetingEmployeeMapper.getById(mOperCode);
        return c;
    }

//    @Value("${file.upload.url}")
//    private String uploadFilePath;
    @Override
    public void getAll() {
        //查询所有科室
        List<MeetingEmployeeDto> emplyeeDept = meetingEmployeeMapper.getAll();
        for (MeetingEmployeeDto meetingEmployeeDto : emplyeeDept) {
            String img1 = meetingEmployeeDto.getDeptCode()+"|硬件|"+meetingEmployeeDto.getDeptName();
//            String img2 = meetingEmployeeDto.getDeptCode()+"|硬件|"+meetingEmployeeDto.getDeptName()64;

            String code1 = QRCodeUtil.getCode(img1);//base64编码软件
//            String code2 = QRCodeUtil.getCode(img2);//base64编码硬件
            byte[] bs1 = new byte[1024];
//            byte[] bs2 = new byte[1024];
            bs1 = Base64.getMimeDecoder().decode(code1);
//            bs2 = Base64.getMimeDecoder().decode(code2);
            BufferedOutputStream bos = null;
            FileOutputStream fos = null;
            File file = null;
            file = new File("D:\\"+"weixiu\\");

            try {
                fos = new FileOutputStream(file+meetingEmployeeDto.getDeptName()+"硬件"+".jpg");
                bos = new BufferedOutputStream(fos);
                bos.write(bs1);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }




        }
        }

    }
}
